<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Booklet extends Controller_DefaultTemplate{

public function action_index()
{
}

public function action_list()
{
	$booklet = ORM::factory('booklet')->find_all();
	$this->template->content = View::factory('book/booklet_list');
	$this->template->content->set('booklet_list', $booklet->as_array());
}

public function action_add()
{
	$this->template->content = View::factory('book/booklet_add');
}

public function action_delete()
{
	$id = $this->request->param('id');
	if ($id)
    {
		$booklet = ORM::factory('booklet')->where('id', '=', $id)->find();
		if ($booklet->loaded())
		{
			$booklet->delete();
		}
		$this->request->redirect('/admin/booklet/list/');
	}
}

public function action_modify()
{
	$id = $this->request->param('id');
	if ($id)
    {
		$booklet = ORM::factory('booklet')->where('id', '=', $id)->find();
		if ($booklet->loaded())
		{
			$this->template->content = View::factory('book/booklet_modify');
			$this->template->content->set('booklet', $booklet);
		}
	}
}

public function action_save()
{
	if ($_POST)
	{
		$id =  arr::get($_POST, 'id');
		$preview =  arr::get($_POST, 'preview');
		$booklet = ORM::factory('booklet');
		if ($id === NULL)
		{
			$booklet->values(array(
				'name' => arr::get($_POST, 'name'),
				'description' => arr::get($_POST, 'description'),
				'preview_url' => arr::get($_POST, 'preview'),
				'description' => arr::get($_POST, 'descriptions'),
				'create_time' => time(),
			));
			$booklet->save();
		}
		else
		{
			$booklet->where('id', '=', $id)->find();
			if ($booklet->loaded())
			{
				$booklet->name = arr::get($_POST, 'name');
				$booklet->description = arr::get($_POST, 'description');
				if ($preview != NULL)
				{
					$booklet->preview_url = $preview;
				}
				$booklet->update();
			}
		}
		$this->request->redirect('/admin/booklet/list/');
		return;
	}
	echo 'fail';
}

}



